package util;

import java.util.Calendar;

public class JDate {
	private int year;
	private int month;
	private int day;
	private int hour;
	private int minute;
	private int second;
	private String date;
	private String time;
	
	public static boolean MySQL = true; 
	
	public JDate() {
		Calendar cal = Calendar.getInstance();
		day = cal.get(Calendar.DAY_OF_MONTH);
		month = cal.get(Calendar.MONTH) + 1;
		year = cal.get(Calendar.YEAR);
		hour = cal.get(Calendar.HOUR_OF_DAY);
		minute = cal.get(Calendar.MINUTE);
		second = cal.get(Calendar.SECOND);
	}
	
	public JDate(int day, int month, int year) {
		setDate(day, month, year);
	}
	
	public JDate(int day, int month, int year, int hour, int minute, int second) {
		setDate(day, month, year, hour, minute, second);
	}
	
	
	public JDate(String date) {
		setDate(date);
	}
	
	public JDate(Calendar cal) {
		setDate(cal);
	}
	
	
	public int getDay() {
		return day;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}
	
	public void setDate(int day, int month, int year) {
		this.day = day;
		this.month = month;
		this.year = year;
		
		date = getDate();
	}
	public void setDate(int day, int month, int year, int hour, int minute, int second) {
		this.day = day;
		this.month = month;
		this.year = year;
		this.hour = hour;
		this.minute = minute;
		this.second = second;

		date = getDate();
		time = getTime();
	}
	
	public void setDate(String date) {
		//System.out.println("fdate: [" + date + "]" );
		if (date.length() > 19) date = date.substring(0, 19);
		
		if (date.indexOf(" ") != -1) {
			String[] times = date.split(" ");
			String time = times[1];
			String tms[] = time.split(":");
			
			this.hour = Integer.valueOf(tms[0]);
			this.minute = Integer.valueOf(tms[1]);
			this.second = Integer.valueOf(tms[2]);
			
			date = times[0];
		}
		
		String[] dts = date.split("-");
	
		if (dts.length == 3) {
			int month = Integer.parseInt(dts[1]);
			int day, year;
			
			if (dts[2].length() == 4) {
				day = Integer.parseInt(dts[0]);
				year = Integer.parseInt(dts[2]);
			}	else {
				day = Integer.parseInt(dts[2]);
				year = Integer.parseInt(dts[0]);
			}
		
			setDate(day, month, year, this.hour, this.minute, this.second);
		}	else {
			System.out.print("Fecha no válida");
		}
	}	
	
	public void setDate(Calendar cal) {
		setDate(cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.YEAR), cal.get(Calendar.HOUR), cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
	}
	
	public String getDate() {
		return getDate(false);
	}
	
	public String getDate(boolean mySQL) {
		String day, month;
		
		if (this.day < 10) day = "0" + this.day;
		else day = String.valueOf(this.day);
		
		int m = this.month;
		if (m < 10) month = "0" + m;
		else month = String.valueOf(m);
		
		if (mySQL) {
			date = String.valueOf(year) + "-" + month + "-" + day;
		}	else {
			date = day + "-" + month + "-" + String.valueOf(year);
		}
		
		return date;
	}
	
	public void setTime(String time) {
		this.time = time;
	}

	public String getTime() {
		time = getTime(false);
		
		return time;
	}
	
	public String getTime(boolean mySQL) {
		String hour, minute, second;
		
		hour = String.valueOf(this.hour);
		if (this.hour < 10) hour = "0" + hour;
		
		minute = String.valueOf(this.minute);
		if (this.minute < 10) minute = "0" + minute;
		
		second = String.valueOf(this.second);
		if (this.second < 10) second = "0" + second;
		
		time = getDate(mySQL) + " " + hour + ":" + minute + ":" + second;
		
		return time;
	}
	
	public int getDaysOfMonth() {
		Calendar cal = Calendar.getInstance();
		cal.set(year, month - 1, day);
		
		return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
	}
	
	public String toString() {
		return getDate();
	}
	
}